package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    BrandMapper bm;


    public PageResult<Brand> queryByManyCase(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        PageHelper.startPage(page,rows);

        // 条件一多，就自己去做一个mapper.xml
        Example example = new Example(Brand.class);
        // 创建where语句
        Example.Criteria criteria = example.createCriteria();
        // 1.key   根据姓名模糊查询
        // 相当于sql语句中   select * from xx where name like %key%  or letter = key
        if(!StringUtils.isEmpty(key)) {
            criteria.andLike("name", "%" + key + "%").orEqualTo("letter",key);
        }
        // 排序是属于example对象  select * from xx order by aaa
        example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));

        List<Brand> list =  bm.selectByExample(example);

        PageInfo<Brand> pages = new PageInfo<>(list);

        return new PageResult<>(pages.getTotal(),pages.getPageSize(),pages.getList());
    }

    @Transactional  // 事务
    public void insertBrand(Brand brand, List<Integer> cids) {
        // insert into brand values(bid,name,letter,image)
        // insert into brand (name,letter) values(#{name},#{letter})
        // 效率高些  useGenerateKey 自动返回id
        bm.insertSelective(brand);


        // cids  分类的id   中间需要的数据   分类的id ， 品牌的id
        for (Integer cid : cids) {
            // 每条数据   cid  bid
            bm.insertCategoryAndBrand(cid,brand.getId());
        }

    }

    @Transactional
    public void update(Brand brand, List<Integer> cids) {
        // 根据商品的id，进行商品信息的修改
        bm.updateByPrimaryKeySelective(brand);

        // 先将中间表中的商品id相关的数据全部删除
        bm.deleteCategoriesAndBrand(brand.getId());

        // 在重新，将cids中的内容插入到中间表
        for (Integer cid : cids) {
            // 每条数据   cid  bid
            bm.insertCategoryAndBrand(cid,brand.getId());
        }
    }

    public List<Brand> getBrandByCid(Integer cid) {
        // 去tb_category_brand 表中，根据category_id 查询对应的brand_id
        // 在根据这个brand_id来到tb_brand表中查询对应的brand对象
        // select brand_id from tb_category_brand where category_id=#{cid}
        // select * from tb_brand where id in
        // (select brand_id from tb_category_brand where category_id=#{cid})
        // sql语句的优化？子查询的效率比较低  ， 联表  内连接查询
        // select * from tb_brand b inner join tb_category_brand c on b.id = c.brand_id where c.category_id = #{cid}

        return bm.queryBrandsByCid(cid);


    }

    public Brand getBrandByBrandId(Long brandId) {
        return bm.selectByPrimaryKey(brandId);
    }
}
